﻿using CommunityToolkit.Mvvm.ComponentModel;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;
using System.Collections.ObjectModel;
using UltrasoundTestingProject.SeedDatas;

namespace UltrasoundTestingProject.Models
{
    /// <summary>
    /// 折线图所需字段实体
    /// </summary>
    public class CartesianChartModel : ObservableObject
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CartesianChartModel()
        {
            var seriesValues = new ObservableCollection<ObservableValue>(SeedData.GetSerieValues());
            _seriesValues = seriesValues;
            _xAxes = GetXAxesOptions(seriesValues.Count);
            _yAxes = GetYAxesOptions(seriesValues.Max(x => x.Value)!.Value);
            _series = GetSeriesOptions(seriesValues);
        }

        /// <summary>
        /// X轴坐标数据
        /// </summary>
        private ObservableCollection<Axis> _xAxes;

        public ObservableCollection<Axis> XAxes
        {
            get { return _xAxes; }
            set { _xAxes = value; }
        }

        /// <summary>
        /// Y轴坐标数据
        /// </summary>
        private ObservableCollection<Axis> _yAxes;

        public ObservableCollection<Axis> YAxes
        {
            get { return _yAxes; }
            set { _yAxes = value; }
        }

        /// <summary>
        /// 数据展示集合
        /// </summary>
        private ObservableCollection<ISeries> _series;

        public ObservableCollection<ISeries> Series
        {
            get { return _series; }
            set { _series = value; }
        }

        /// <summary>
        /// 折线图的数据集合
        /// </summary>
        private ObservableCollection<ObservableValue> _seriesValues;

        public ObservableCollection<ObservableValue> SeriesValues
        {
            get { return _seriesValues; }
            set { _seriesValues = value; }
        }

        #region 公开方法

        public ObservableCollection<ObservablePoint> GetSecondAxis()
        {
            return new ObservableCollection<ObservablePoint>()
            {
                new ObservablePoint(0,40),
                new ObservablePoint(100,40)
            };
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 获取xAxes配置
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        private ObservableCollection<Axis> GetXAxesOptions(int count)
        {
            return new ObservableCollection<Axis>()
            {
                new Axis()
                {
                    MinLimit = 0,
                    MaxLimit = Math.Ceiling(count / 50.0) * 50.0,
                    MinStep = 50,
                    LabelsPaint = new SolidColorPaint(SKColors.White) { StrokeThickness = 1 },
                    SeparatorsPaint = new SolidColorPaint(SKColors.Transparent) { StrokeThickness = 1 }
                }
            };
        }

        /// <summary>
        /// 获取yAxes配置
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        private ObservableCollection<Axis> GetYAxesOptions(double maxValue)
        {
            return new ObservableCollection<Axis>()
            {
                new Axis()
                {
                    MinLimit = 0,
                    MaxLimit = (double)Math.Ceiling(maxValue / 50.0) * 50.0,
                    MinStep = 50,
                    LabelsPaint = new SolidColorPaint(SKColors.White) { StrokeThickness = 1 },
                    SeparatorsPaint = new SolidColorPaint(SKColors.Transparent) { StrokeThickness = 1 }
                }
            };
        }

        /// <summary>
        /// 获取序列集合
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        private ObservableCollection<ISeries> GetSeriesOptions(ObservableCollection<ObservableValue> values)
        {
            return new()
            {
                GetFirstAxisOptions(values),
                GetSecondAxisOptions()
            };
        }

        /// <summary>
        /// 获取第一个轴展示数据（曲线 —— 塌陷程度）
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        private LineSeries<ObservableValue> GetFirstAxisOptions(ObservableCollection<ObservableValue> values)
        {
            return new LineSeries<ObservableValue>
            {
                Values = values, // 呈现数据集合
                Stroke = new SolidColorPaint(SKColors.Green) { StrokeThickness = 2 },  // 线的粗细
                Fill = null,  // 填充
                GeometryFill = null,  // 圆形填充
                GeometryStroke = null   // 圆形的粗细
            };
        }

        /// <summary>
        /// 获取第二轴展示数据（直线 —— 闸门）
        /// </summary>
        /// <returns></returns>
        private LineSeries<ObservablePoint> GetSecondAxisOptions()
        {
            return new LineSeries<ObservablePoint>
            {
                LineSmoothness = 1,  // 直线
                Values = GetSecondAxis(), // 呈现数据集合
                Stroke = new SolidColorPaint(SKColors.Red) { StrokeThickness = 2 },  // 线的粗细
                Fill = null,  // 填充
                GeometryFill = null,  // 圆形填充
                GeometryStroke = null   // 圆形的粗细
            };
        }

        #endregion
    }
}
